<template>
<div>
	<div class="add_container" v-show= !this.onshow>
		<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

			<el-form-item label="生日" required>
				<el-col :span="11">
					<el-form-item prop="date">
						<el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.birthday" style="width: 100%;"></el-date-picker>
					</el-form-item>
					

				</el-col>
			</el-form-item>

			<el-form-item label="姓名" prop="name">
				<el-input v-model="ruleForm.name"></el-input>
			</el-form-item>

			<el-form-item label="省份" prop="prov">
				<el-input v-model="ruleForm.prov"></el-input>
			</el-form-item>

			<el-form-item label="城市" prop="city">
				<el-input v-model="ruleForm.city"></el-input>
			</el-form-item>
			<el-form-item label="地址" prop="address">
				<el-input v-model="ruleForm.address"></el-input>
			</el-form-item>
			<el-form-item label="邮政编码" prop="expressid">
				<el-input v-model="ruleForm.expressid"></el-input>
			</el-form-item>




			<el-form-item>
				<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
				<el-button @click="resetForm('ruleForm')">重置</el-button>
			</el-form-item>
		</el-form>

	</div>
	
	
<div class="index_container" v-show= this.onshow>
	<el-button type="primary" @click=togggle>添加</el-button>
	<el-table :data="tableData"  border style="width: 100%" >
		<el-table-column fixed="left"  type="index" width="50">
		</el-table-column>
		<el-table-column prop="birthday" label="生日" width="150">
		</el-table-column>
		<el-table-column prop="name" label="姓名" width="120">
		</el-table-column>
		<el-table-column prop="prov" label="省份" width="120">
		</el-table-column>
		<el-table-column prop="city" label="市区" width="120">
		</el-table-column>
		<el-table-column prop="address" label="地址" width="300">
		</el-table-column>
		<el-table-column prop="expressid" label="邮编" width="120">
		</el-table-column>
		<el-table-column fixed="left" label="操作" width="100">
			<template slot-scope="scope">
				<el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
				<el-button @click="updateClick(scope.row)"  type="text" size="small">编辑</el-button>
			</template>
		</el-table-column>
	</el-table>
</div>
</div>
</template>

<script>
import axios from 'axios'
	export default {
		name: 'index',
		data() {
			return {
				id:'',
				onshow: true,
				tableData: [{
					date: '',
					name: '',
					prov: '',
					city: '',
					address: '',
					express: ''
				}],
				ruleForm: {
					name: '',
					birthday: '',
					prov: '',
					city: '',
					address: '',
					expressid: ''
				
				},
				rules: {
					// type: 'date',
					birthday: [{
						required: true,
						type: 'date',
						message: '请选择日期',
						trigger: 'change'
					}],
					name: [{
							required: true,
							message: '请输入姓名',
							trigger: 'blur'
						},
						{
							min: 2,
							max: 15,
							message: '长度在 2 到 15 个字符',
							trigger: 'blur'
						}
					],
					prov: [{
							required: true,
							message: '请输入省份',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 15,
							message: '长度在 3 到 15 个字符',
							trigger: 'blur'
						}
					],
					city: [{
							required: true,
							message: '请输入城市',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 15,
							message: '长度在 3 到 15 个字符',
							trigger: 'blur'
						}
					],
					address: [{
							required: true,
							message: '请输入地址',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 15,
							message: '长度在 3 到 15 个字符',
							trigger: 'blur'
						}
					],
					expressid: [{
							required: true,
							message: '请输入活动名称',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 6,
							message: '长度为  6 个字符',
							trigger: 'blur'
						}
					]
				
				}
			}
		},
		methods: {
			togggle(){
				this.$router.push('/add')
			},
			handleClick(row) {
				console.log(row);
				const obj = {
					'id': row._id ,		
				}
				this.$http.post("http://localhost:3000/delete",obj).then((res)=>{
					console.log(res)
				})
				// 删除后  刷新页面
				this.$http.get("http://localhost:3000/students").then((res)=>{
				//console.log(res)
				this.tableData = res.data
				})
			},
			updateClick(row){
				this.onshow = false
				console.log(row._id)
				this.id = row._id
				const obj1 = {
					'id': row._id ,		
				}
                let res = this.$http.post("http://localhost:3000/student",obj1).then((res)=> {
                
                		console.log(res)
                		this.ruleForm = res.data
                		console.log('按id查找成功')
						//this.onshow = true      	
                }
                )
			},
			submitForm(ruleForm) {
				this.$refs[ruleForm].validate((valid) => {
					if (valid) {
						// 修改学生信息
						console.log(this.ruleForm)
						const obj = {
							id: this.id,
							name: this.ruleForm.name,
							prov: this.ruleForm.prov,
							address: this.ruleForm.address,
							expressid: this.ruleForm.expressid,
							birthday: this.ruleForm.birthday,
							city: this.ruleForm.city
						}
						let res = this.$http.post("http://localhost:3000/update", obj).then((res)=> {
							if(res.ok){
								console.log('编辑成功')
								
							}
						})
						//编辑成功 后更新数据
						this.$http.get("http://localhost:3000/students").then((res)=>{
						//console.log(res)
						this.tableData = res.data
						})
						
						this.onshow = true 
						//let res = this.$http.post("http://localhost:3000/add", this.ruleForm);
						alert('submit!');
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			
			}
			
			
		},

         created(){
			 this.$http.get("http://localhost:3000/students").then((res)=>{
			 //console.log(res)
			 this.tableData = res.data
			 })
		 }
		

	}
</script>
<style scoped>
	.index_container{
		margin-top: 30px;
		width: 80%;
		/* position: absolute; */
		margin-left: 10%;
	}
</style>